package view.cms
{
	import business.CMSDelegate;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.TitleWindow;
	import mx.controls.Alert;
	import mx.controls.Label;
	import mx.rpc.Fault;
	import mx.rpc.events.FaultEvent;
	import mx.utils.ObjectUtil;
	
	public class PreviewCptClass extends TitleWindow
	{
		public var delegate:CMSDelegate;
		
		public var prompt:Label;
		
		[Bindable]
		private  var _previewArr:ArrayCollection
		
		[Bindable]
		public var gridArr:ArrayCollection;
		
		public function PreviewCptClass()
		{
			super();
			delegate = new CMSDelegate(this);
		}
		
		public function addPreview(event:MouseEvent):void
		{
			var obj:Object = ObjectUtil.copy(gridArr.getItemAt(0));
			obj["time"] = previewArr.getItemAt(0).time;
			gridArr.addItem(obj);
		}
		
		public function initGridData(obj:Object):void
		{
			gridArr = new ArrayCollection([obj]);
			delegate.loadBookingInfo(obj["pageId"], obj["subId"], obj["pos"]);
		}
		
		public function loadBookingInfoResult(result:Object, token:Object = null):void
		{
			result = result.result;
			if ( result.code )
			{
				var fault:Fault = new Fault('',result.msg);
				var error:FaultEvent = new FaultEvent('fault',false,true,fault);
				this.fault(error);
				return;
			}
			if(result.data != null)
			{
				var arr:Array = (result.data as Array);
				for(var i:int=0; i<arr.length; i++)
				{
					gridArr.addItem(arr[i]);	
				}
			}
		}
		
		public function saveBookingInfo(arr:Array):void
		{
			delegate.saveBookingInfo(arr);
		}
		
		public function saveBookingInfoResult(result:Object, token:Object = null):void
		{
			result = result.result;
			if ( result.code )
			{
				var fault:Fault = new Fault('',result.msg);
				var error:FaultEvent = new FaultEvent('fault',false,true,fault);
				this.fault(error);
				return;
			}
			prompt.text = "保存成功！";
		}
		
		public function fault(error:FaultEvent,token:Object = null):void
		{
			Alert.show(error.fault.faultString,'error');
		}
		
		public function set previewArr(ac:ArrayCollection):void
		{
			_previewArr = ac;
		}
		
		[Bindable]
		public function get previewArr():ArrayCollection
		{
			return _previewArr;
		}
	}
}